<template>
    <div class="mine-page">
      <!-- 页面主体内容 -->
      <div class="page-content">
        <!-- 用户头像信息部分 -->
        <div class="content-userInfo">
          <img class="userInfo-avatar" :src="userInfo.avatar" alt="" @click="goUserInfoPage_btn">
          <div class="userInfo-info" @click="goUserInfoPage_btn">
            <span class="info-nickname">{{ userInfo.nickname }}</span>
            <span class="info-username">{{ userInfo.username }}</span>
          </div>
          <div class="userInfo-setting" @click="goSetting_btn">
            <SvgIcon class="setting-icon" name="setting"></SvgIcon>
            <span class="setting-text">设置</span>
          </div>
        </div>
        <!-- 相关功能区 -->
        <div class="content-functional">
          <div class="functional-item">
            <SvgIcon name="collection" width="1em" height="1em" color="#221eea"></SvgIcon>
            <span class="item-tip">收藏</span>
          </div>
          <div class="functional-item">
            <SvgIcon name="store" width="1em" height="1em" color="#221eea"></SvgIcon>
            <span class="item-tip">关注店铺</span>
          </div>
          <div class="functional-item">
            <SvgIcon name="footprint" width="1.2em" height="1.2em" color="#221eea"></SvgIcon>
            <span class="item-tip">足迹</span>
          </div>
          <div class="functional-item">
            <SvgIcon name="production" width="0.9em" height="0.9em" color="#221eea"></SvgIcon>
            <span class="item-tip">作品</span>
          </div>
        </div>
        <!-- 我的订单信息部分 -->
        <div class="content-order">
          <div class="order-title">
            <span class="title-tip">我的订单</span>
            <div class="title-right" @click="goOrderListPage_btn('全部')">
              <span class="right-all">全部</span>
              <van-icon name="arrow" size="12" color="#9195a3"/>
            </div>
          </div>
          <div class="order-content">
            <div class="content-item" @click="goOrderListPage_btn('待付款')">
              <SvgIcon class="item-icon" name="order-1" width="1.2em" height="1.2em" color="#000"></SvgIcon>
              <span class="item-text">待付款</span>
            </div>
            <div class="content-item" @click="goOrderListPage_btn('待发货')">
              <SvgIcon class="item-icon" name="order-2" width="1.1em" height="1.1em" color="#000"></SvgIcon>
              <span class="item-text">待发货</span>
            </div>
            <div class="content-item" @click="goOrderListPage_btn('待收货')">
              <SvgIcon class="item-icon" name="order-3" width="1.1em" height="1.1em" color="#000"></SvgIcon>
              <span class="item-text">待收货</span>
            </div>
            <div class="content-item" @click="goOrderListPage_btn('待评价')">
              <SvgIcon class="item-icon" name="order-4" width="1.1em" height="1.1em" color="#000"></SvgIcon>
              <span class="item-text">待评价</span>
            </div>
            <div class="content-item" @click="goOrderListPage_btn('售后')">
              <SvgIcon class="item-icon" name="order-5" width="1.1em" height="1.1em" color="#000"></SvgIcon>
              <span class="item-text">退款/售后</span>
            </div>
          </div>
        </div>
        <!-- 钱包静态展示 -->
        <div class="content-wallet">
          <span class="wallet-title">星辰钱包</span>
          <div class="wallet-right">
            <span class="wallet-tip">查看余额</span>
            <van-icon name="arrow" size="12" color="#9195a3"/>
          </div>
        </div>
        <div class="content-options">
          <div class="options-item">
            <SvgIcon class="item-icon" name="cash" width="1.3em" height="1.3em" color="#f1d239"></SvgIcon>
            <span class="item-text">现金大转盘</span>
          </div>
          <div class="options-item">
            <SvgIcon class="item-icon" name="pasture" width="1.3em" height="1.3em" color=""></SvgIcon>
            <span class="item-text">星辰牧场</span>
          </div>
          <div class="options-item">
            <SvgIcon class="item-icon" name="active" width="1.3em" height="1.3em" color=""></SvgIcon>
            <span class="item-text">新客立减</span>
          </div>
          <div class="options-item">
            <SvgIcon class="item-icon" name="train" width="1.3em" height="1.3em" color=""></SvgIcon>
            <span class="item-text">火车票</span>
          </div>
          <div class="options-item" @click="goAddress_btn">
            <SvgIcon class="item-icon" name="address" width="1.3em" height="1.3em" color=""></SvgIcon>
            <span class="item-text">收货地址</span>
          </div>
          <div class="options-item">
            <SvgIcon class="item-icon" name="service" width="1.3em" height="1.3em" color=""></SvgIcon>
            <span class="item-text">官方客服</span>
          </div>
          <div class="options-item">
            <SvgIcon class="item-icon" name="express" width="1.3em" height="1.3em" color=""></SvgIcon>
            <span class="item-text">快递</span>
          </div>
          <div class="options-item" @click="goSetting_btn">
            <SvgIcon class="item-icon" name="install" width="1.3em" height="1.3em" color=""></SvgIcon>
            <span class="item-text">设置</span>
          </div>
        </div>
      </div>
      <!-- 底部tabBar -->
      <TabBar></TabBar>
    </div>
</template>

<script setup lang="ts">
//引入vue组合式函数
import { ref, reactive, onMounted } from "vue";
//引入组件
import TabBar from '@/components/TabBar/TabBar.vue';
//引入vant组件
import { Icon as VanIcon, showToast } from "vant";
//@ts-ignore引入useUserStore
import { useUserStore } from "@/store/userStore.ts";
const userStore = useUserStore();
//引入路由组件vue-router
import { useRoute, useRouter } from "vue-router";
const router = useRouter();
//@ts-ignore引入请求api
import { reqUserInfo } from "@/api/user/index.ts";

//登录用户的相关信息
const userInfo = reactive({
  id:0,
  username:"",
  password:"",
  nickname:"",
  email:"",
  phone:"",
  avatar:"",
  createTime:"",
  updateTime:""
})

//获取用户信息的相关函数
async function getUserInfo () {
  //用户仓库--本地存储--发请求
  const storeUserInfo = JSON.parse(localStorage.getItem("userInfo"));  //本地存储
  if(userStore.userInfo.username !== "" && userStore.userInfo.username !== undefined){
    userInfo.id = userStore.userInfo.id;
    userInfo.username = userStore.userInfo.username;
    userInfo.password = userStore.userInfo.password;
    userInfo.nickname = userStore.userInfo.nickname;
    userInfo.email = userStore.userInfo.email;
    userInfo.phone = userStore.userInfo.phone;
    userInfo.avatar = userStore.userInfo.avatar;
  } else if (storeUserInfo !== null && storeUserInfo !== undefined) {
    userInfo.id = storeUserInfo.id;
    userInfo.username = storeUserInfo.username;
    userInfo.password = storeUserInfo.password;
    userInfo.nickname = storeUserInfo.nickname;
    userInfo.email = storeUserInfo.email;
    userInfo.phone = storeUserInfo.phone;
    userInfo.avatar = storeUserInfo.avatar;
  } else {
    //仓库、本地存储均没有数据，发请求获取数据
    let res = await reqUserInfo(localStorage.getItem("username"));
    if(res.code === 200){
      userInfo.id = res.result.id;
      userInfo.username = res.result.username;
      userInfo.password = res.result.password;
      userInfo.nickname = res.result.nickname;
      userInfo.email = res.result.email;
      userInfo.phone = res.result.phone;
      userInfo.avatar = res.result.avatar;
      showToast("用户信息获取成功!");
    }
    console.log('获取前台用户信息api',res);
  }
}

//页面跳转:跳转到订单列表页(全部)
function goOrderListPage_btn (state?:string) {
  if(state === "全部"){
    router.push("/orderList");
  } else {
    router.push({path:"/orderList",query:{state:state}});
  }
}
//页面跳转:跳转到设置页
function goSetting_btn () {
  router.push("/setting");
}
//页面跳转:跳转到收货地址页
function goAddress_btn () {
  router.push("/address");
}
//页面跳转:跳转到用户信息页
function goUserInfoPage_btn () {
  router.push("/user");
}

//vue生命周期
onMounted(() => {
  getUserInfo();  //获取用户信息
})
</script>

<style lang="less" scopde>
.mine-page{
  width: 375px;
  height: calc(100vh - 50px);
  /**页面主体内容 */
  .page-content{
    width: 100%;
    height: calc(100vh - 60px);
    overflow: auto;
    /**用户头像信息部分 */
    .content-userInfo{
      width: 100%;
      height: 120px;
      padding: 20px;
      box-sizing: border-box;
      display: flex;
      align-items: flex-start;
      background: linear-gradient(to right,#f8fdec,#dfeecb);
      .userInfo-avatar{
        width: 70px;
        height: 70px;
        margin-right: 10px;
        //background: #919191;
        border-radius: 50%;
      }
      .userInfo-info{
        width: 200px;
        height: 60px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .info-nickname{
          font-size: 18px;
          color: #000;
          font-weight: 600;
        }
        .info-username{
          font-size: 12px;
          color: #9195a3;
        }
      }
      .userInfo-setting{
        width: 50px;
        height: 20px;
        font-size: 10px;
        position: relative;
        top:50px;
        display: flex;
        align-items: center;
        color:#9195a3;
      }
    }
    /**相关功能区 */
    .content-functional{
      width: 100%;
      height: 70px;
      background: #fbf6f6;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .functional-item{
        color: #000;
        display: flex;
        flex-direction: column;
        align-items: center;
        .item-tip{
          font-size: 12px;
        }
      }
    }
    /**订单信息部分 */
    .content-order{
      width: 100%;
      padding: 10px;
      box-sizing: border-box;
      border-bottom: 1px solid #f1f2f0;
      .order-title{
        width: 100%;
        height: 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .title-tip{
          font-size: 16px;
          font-weight: 550;
        }
        .title-right{
          display: flex;
          .right-all{
            font-size: 12px;
            color:#919191;
            cursor: pointer;
          }
        }
      }
      .order-content{
        width: 100%;
        height: 60px;
        display: flex;
        justify-content: space-around;
        .content-item{
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .item-text{
            font-size: 12px;
          }
        }
      }
    }
    /**钱包静态展示 */
    .content-wallet{
      width: 100%;
      height: 55px;
      padding: 10px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #f1f2f0;
      .wallet-title{
        font-size: 16px;
        color: #000;
        font-weight: 560;
      }
      .wallet-right{
        display: flex;
        align-items: center;
        .wallet-tip{
          font-size: 12px;
          color: #9195a3;
        }
      }
    }
    /**相关功能区2 */
    .content-options{
      width: 375px;
      margin: 5px 0;
      display: flex;
      flex-wrap: wrap;
      border-bottom: 1px solid #f1f2f0;
      .options-item{
        width: 65px;
        margin: 12px 5px;
        display: flex;
        flex-direction: column;
        align-items: center;
        .item-text{
          font-size: 12px;
        }
      }
    }
  }
}
</style>